# -*- coding: UTF-8 -*-

import datetime
import sys
from sqlalchemy import and_,  func, or_
from typing import Dict, List, Any, Generator

from sqlalchemy import Boolean, Column, DateTime, Integer, String, Index, Table, ForeignKey, text, JSON, TEXT, distinct
from sqlalchemy.orm import Session, relationship
from common.database import Base
from common.database import SessionMixin
from common.utils import is_empty
from core.logger import logger



__all__ = ['ManageNoticeTeam']

class ManageNoticeTeam(Base, SessionMixin):
    '''
    通知组
    '''
    __tablename__ = "manage_notice_team"
    name = Column(String(64), nullable=False, comment='通知组名')
    comment = Column(String(256), nullable=True, comment='通知组的说明')


    __table_args__ = (
        Index('idx_manage_notice_team_name', 'name', unique=True),
        {
            'mysql_engine': 'innodb',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_unicode_ci',
            'extend_existing': True,
            'mysql_row_format': 'dynamic'
        }
    )

    def __repr__(self):
        return f"<manage_team_name='{self.name}'>"


    def object_to_dict(self, db: Session):
        result = {
            key: getattr(self, key)
            for key in self.__mapper__.c.keys()
        }
        result["users"] = [
            user.object_to_dict(db)
            for user in self.users
        ]

        return result



    @classmethod
    def delete_object(cls, db: Session, uuid: int):
        notice_team = cls.get_object_by_uuid(db, uuid)
        notice_team.users = []
        db.commit()
        notice_team.delete(db)

